package com.adams.action.sys;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import com.adams.action.view.ViewAction;
import com.adams.common.BaseAction;
import com.adams.common.Constants;
import com.adams.custom.page.mgt.service.ViewParseService;
import com.adams.custom.page.util.ViewConstant;

/**
 * 
 * 
 * File Name : MenuAction.java
 * 
 * @Description : 菜单管理类
 * @author 902990封文进
 */
public class MenuAction extends BaseAction
{

	private ViewParseService viewParseService;

	private int menuId;

	private String gridPanelId;

	private String viewEventId;

	private String formPanelId;

	private String viewItemId;

	private String searchPanelId;

	/**
	 * 
	 * Description : 显示布局
	 * 
	 * @return String
	 * @throws Exception
	 * 
	 */
	@SuppressWarnings("unchecked")
	public String listPanel() throws Exception
	{
		// Map map = viewParseService.getParseViewByMenuId(getUserId(), menuId);
		// write(JSONObject.fromObject(map).toString());
		write(viewParseService.getParseViewJSONByMenuId(getUserId(), menuId));
		return null;
	}

	/**
	 * Description : 显示查询条字段信息
	 * 
	 * @return
	 * @throws Exception
	 * 
	 */
	public String listSearchItem() throws Exception
	{
		// Map map = viewParseService.getParseSearchItems(getUserId(), menuId,
		// searchPanelId);
		// write(JSONObject.fromObject(map).toString());
		write(viewParseService.getParseSearchItemsJSON(getUserId(), menuId,
				searchPanelId));
		return null;
	}

	/**
	 * 
	 * Description : 显示元素
	 * 
	 * @return String
	 * @throws Exception
	 * 
	 */
	public String listGridStructure() throws Exception
	{
		// Map map = viewParseService.getParseGrids(getUserId(), gridPanelId);
		// write(JSONObject.fromObject(map).toString());
		write(viewParseService.getParseGridsJSON(getUserId(), gridPanelId));
		return null;
	}

	/**
	 * 
	 * Description : 显示数据
	 * 
	 * @return String
	 * @throws Exception
	 * 
	 */
	@SuppressWarnings("unchecked")
	public String listGridData() throws Exception
	{
		Map inputMap = ViewAction.requestParameterMap2Map(request);
		Map map = viewParseService.getParseGridDatas(getUserId(), gridPanelId,
				searchPanelId, inputMap);
		write(JSONObject.fromObject(map).toString());
		return null;
	}

	/**
	 * 
	 * Description : 打开面板窗口
	 * 
	 * @return String
	 * @throws Exception
	 * 
	 */
	@SuppressWarnings("unchecked")
	public String openPanelWindow() throws Exception
	{

		List<Map> mapList = ViewAction.getDataMap(request);
		Map inputMap = null;
		if (mapList.size() > 0)
		{
			inputMap = ViewAction.getDataMap(request).get(0);
		}
		Map map = viewParseService.getParseViewByEventId(getUserId(), menuId,
				viewEventId, inputMap);
		write(JSONObject.fromObject(map).toString());

		return null;

	}

	/**
	 * 
	 * Description : 查询父菜单
	 * 
	 * @return String
	 * @throws Exception
	 * 
	 */
	@SuppressWarnings("unchecked")
	public String queryParentMenu() throws Exception
	{

		Map map = viewParseService.getParseDynamicCombo(getUserId(), menuId,
				viewItemId);
		Map combo = (Map) map.get(ViewConstant.VIEW_DYNAMICCOMBO_DATA);
		List<Map> records = new ArrayList<Map>();
		List<Map> temp = (List<Map>) combo
				.get(ViewConstant.VIEW_DYNAMICCOMBO_COMBO);
		Map nullMap = new HashMap();
		nullMap.put("id", 0);
		nullMap.put("menuName", "root");
		records.add(nullMap);
		records.addAll(temp);
		combo.put(ViewConstant.VIEW_DYNAMICCOMBO_COMBO, records);
		map.put(ViewConstant.VIEW_DYNAMICCOMBO_DATA, combo);
		write(JSONObject.fromObject(map).toString());
		return null;
	}

	/**
	 * 
	 * Description : 添加菜单
	 * 
	 * @return String
	 * @throws Exception
	 * 
	 */
	@SuppressWarnings("unchecked")
	public String add() throws Exception
	{

		Map inputMap = ViewAction.requestParameterMap2Map(request);
		Map map = viewParseService.saveOrUpdateDataInViewForm(getUserId(),
				menuId, formPanelId, inputMap);
		write(JSONObject.fromObject(map).toString());

		// 记录日志
		createLog("addMenu", "menu name:" + inputMap.get("menuName"));
		return null;
	}

	/**
	 * 
	 * Description : 更新菜单
	 * 
	 * @return String
	 * @throws Exception
	 * 
	 */
	@SuppressWarnings("unchecked")
	public String update() throws Exception
	{

		Map inputMap = ViewAction.requestParameterMap2Map(request);
		Map map = viewParseService.saveOrUpdateDataInViewForm(getUserId(),
				menuId, formPanelId, inputMap);
		write(JSONObject.fromObject(map).toString());

		// 记录日志
		createLog("updateMenu", "menu name:" + inputMap.get("menuName"));
		return null;
	}

	/**
	 * Description : 更新数据
	 * 
	 * @return
	 * @throws Exception
	 * 
	 */
	@SuppressWarnings("unchecked")
	public String updateDataByGridPanel() throws Exception
	{
		Map inputMap = ViewAction.requestParameterMap2Map(request);
		List<Map> mapList = new ArrayList<Map>();
		mapList.add(inputMap);
		Map map = viewParseService.saveOrUpdateDataInGridPanel(getUserId(),
				menuId, viewEventId, mapList);
		write(JSONObject.fromObject(map).toString());
		return null;
	}

	/**
	 * 
	 * Description : 删除菜单
	 * 
	 * @return String
	 * @throws Exception
	 * 
	 */
	@SuppressWarnings("unchecked")
	public String delete() throws Exception
	{
		List<Map> mapList = ViewAction.getDataMap(request);
		viewParseService
				.deleteDataByViewForm(getUserId(), viewEventId, mapList);

		// 记录日志
		StringBuilder menuNames = new StringBuilder();
		for (Map map : mapList)
		{
			menuNames.append(map.get("menuName") + ",");
		}
		createLog("deleteMenu", "menu name:"
				+ menuNames.substring(0, menuNames.length() - 1));

		sendSuccessMessage(Constants.DELETE_SUCCESS);
		return null;
	}

	/**
	 * Description : 得到菜单详细数据
	 * 
	 * @return String
	 * @throws Exception
	 * 
	 */
	@SuppressWarnings("unchecked")
	public String get() throws Exception
	{

		List<Map> mapList = ViewAction.getDataMap(request);
		Map inputMap = null;
		if (mapList.size() > 0)
		{
			inputMap = ViewAction.getDataMap(request).get(0);
		}
		Map map = viewParseService.getDataByViewForm(getUserId(), menuId,
				formPanelId, inputMap);
		write(JSONObject.fromObject(map).toString());
		return null;
	}

	// /////////////////////属性设置/////////////////////////

	public void setMenuId(int menuId)
	{
		this.menuId = menuId;
	}

	public void setViewParseService(ViewParseService viewParseService)
	{
		this.viewParseService = viewParseService;
	}

	public void setViewEventId(String viewEventId)
	{
		this.viewEventId = viewEventId;
	}

	public void setViewItemId(String viewItemId)
	{
		this.viewItemId = viewItemId;
	}

	public void setFormPanelId(String formPanelId)
	{
		this.formPanelId = formPanelId;
	}

	public void setGridPanelId(String gridPanelId)
	{
		this.gridPanelId = gridPanelId;
	}

	public void setSearchPanelId(String searchPanelId)
	{
		this.searchPanelId = searchPanelId;
	}
}
